package helper;
//class that deals with format and such
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;

import databeans.Payment;
import databeans.Transaction;

public class Formatter {
    
    public static final DecimalFormat cashDf = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.US));
    public static final DecimalFormat sharesDf = new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.US));
    public static final DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    
    public static String prepCashToView (long number){
        double prepResult = Double.parseDouble(Long.toString(number));
        prepResult = prepResult/100; //to cents
        return cashDf.format(prepResult);
    }
    
    public static String prepShareToView (long number){
        double prepResult = Double.parseDouble(Long.toString(number));
        prepResult = prepResult/1000; //to 3dp
        return sharesDf.format(prepResult);
    }
    
    public static long prepCashFromView (double number){

        double tempDouble = number * 100;
        long tempLong = (long) tempDouble;
        return tempLong;
    }
    
    public static long prepCashFromView (String number){
        double temp= Double.parseDouble(number);
        
        return prepCashFromView(temp);
    }
    
    public static long prepShareFromView (double number){
        double tempDouble = number * 1000;
        long tempLong = (long) tempDouble;
        return tempLong;
    }
    
    public static long prepShareFromView (String number){
        //long result;   
        double temp= Double.parseDouble(number);
        
        return prepShareFromView(temp);
    }
    
    public static boolean isEmail(String email) {  
        if(email==null)  {   
            return false;  
        } 
        String emailPattern="^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})";  
        return email.matches(emailPattern);  
    }
    
    public static boolean isInteger( String input )
    {
       try
       {
          Integer.parseInt( input.trim() );
          return true;
       }
       catch( Exception e)
       {
          return false;
       }
    }
    
    public static boolean isDouble ( String input )
    {
       try
       {
          Double.parseDouble( input.trim() );
          return true;
       }
       catch( Exception e)
       {
          return false;
       }
    }

    public static int numberOfDecimal (String input) {
        String [] res = new String[2];
        res = input.split("\\.");
        
        try {
            if (res[1]!=null){
                return res[1].length();
        
            } else
            {
                return 0;
            }
            
        }
        catch( Exception e)
        {
           return 0;
        }

    }
    
    public static int numberOfDecimal (Double input) {
       
        String s = Double.toString(input);
        return Formatter.numberOfDecimal(s);

    }
    
    public static int numberOfDecimal (Long input) {
        
        String s = String.valueOf(input);
        return Formatter.numberOfDecimal(s);

    }
    
    public static String transactionType(String input){
		
    	if(input.equals(Transaction.BUY_ORDER))
			return "Buy";
		else if(input.equals(Transaction.SELL_ORDER))
			return "Sell";
		else if(input.equals(Transaction.CHECK_DEPOSIT))
			return "Deposit";
		else if(input.equals(Transaction.CHECK_REQUEST))
			return "Withdrawal";
          //else if(input.equals(Payment.DEPOSIT_CHECK))
          //      return "Deposit";
          //else if(input.equals(Payment.WITHDRAW_FUND))
          //    return "Withdrawal";
          else if(input.equals(Transaction.EXCHANGE_BUY_ORDER))
              return "Exchange Buy";
          else if(input.equals(Transaction.EXCHANGE_SELL_ORDER))
              return "Exchange Sell";
		return "";
    }
}
